/**
 * 阅读
 * @author Qasim <15750783791@163.com>
 * @version 2016/11/1
 */


document.addEventListener('plusready' , ready , false);
var self = memberInfo = id = isbuy = '';
function ready(){
	self = plus.webview.currentWebview();
	$('.content').html(self.content);
	id = self.aid;
	isbuy = self.isbuy;
	var fontsize = plus.storage.getItem('fontsize');
	var fontfamily = plus.storage.getItem('fontfamily');
	var background_color = plus.storage.getItem('background');
	if(background_color){
		$('body .mui-content').css('background-color' , background_color);
	}
	
	//上一章
	$('.previous').on('tap' , function(){
		books('previous');
	})
	
	//下一章
	$('.next').on('tap' , function(){
		books('next');
	}) 
	
	
	//设置字号
	$('.font_size button').on('click' , function(){
		var index = $('.font_size button').index($(this));
		var fontsize = '14px';
		if(index == 0){
			fontsize = '16px';
		}else if(index == 1){
			fontsize = '14px';
		}else{
			fontsize = '12px';
		}
		$(this).siblings().css('border-color' , '#ffffff');
		$(this).css('border-color' , '#139f13')
		plus.storage.setItem('fontsize' , fontsize);
		$('#content').css('font-size' , fontsize);
		$('#content p').css('font-size' , fontsize); 
	})
	
	
	//设置字体
	$('.font_face button').on('click' , function(){
		var index = $('.font_face button').index($(this));
		var fontfamily = 'timesNewRoman';
		if(index == 0){
			fontfamily = 'timesNewRoman';
		}else if(index == 1){
			fontfamily = 'Arial';
		}else{
			fontfamily = 'cambria';
		}
		$(this).siblings().css('border-color' , '#ffffff');
		$(this).css('border-color' , '#139f13')
		plus.storage.setItem('fontfamily' , fontfamily);
		$('#content').css('font-family' , fontfamily);
		$('#content p').css('font-family' , fontfamily); 
	})
	
	
	//设置背景 
	$('.background span').on('click' , function(){
		var index = $('.background span').index($(this));
		var background_color = '#cdefce';
		if(index == 0){
			background_color = '#cdefce';
		}else if(index == 1){
			background_color = '#cdd2ef';
		}else if(index == 2){
			background_color = '#efedcd';
		}else{
			background_color = '#efcfcd';
		}
		plus.storage.setItem('background' , background_color);
		$('body .mui-content').css('background-color' , background_color);
	})
	
	/**
	 * 监听点击坐标
	 */
	document.getElementById('content').addEventListener('touchstart',function(e){
		var body_width = Number(document.body.clientWidth);
		var touch = e.touches[0]; //获取第一个触点
        var x = Number(touch.pageX); //页面触点X坐标
        var y = Number(touch.pageY); //页面触点Y坐标
		if(x < 50){
			books('previous');
		}else if((body_width - x) < 50){
			books('next');
		}
	});
	
	$('.book_name').text(self.name)
	if(self.chapter){
		$('.name').text(self.chapter.single.chapter_name)
		$('#content').html(self.chapter.single.content);
		var ordinal =  plus.storage.getItem('chapter'+id);
		$('.chapter_num').text(self.chapter.all.length)
		$.each(self.chapter.all, function(k, v) {
			var html = '<li class="mui-table-view-cell chapter'+v.ordinal+'" data-ordinal="'+v.ordinal+'" ><label style="color:'+(v.ordinal == parseInt(ordinal) ? "green;" : "#8f8f94;")+'">'+v.chapter_name+'</label>';
			if(k > 0 ){  
				if(!isbuy && self.pattern == '收费'){
					html += '<span class="mui-pull-right mui-h5"><i class="mui-icon iconfont">&#xe621;</i></span>';
				}
			} 
			html += '</li>';
			$('.list').append(html)
		});
		
		$('.list li').unbind();
		$('.list li').on('click' , function(){
			if(isbuy || self.pattern == '免费'){
				plus.storage.setItem('chapter'+id , $(this).attr('data-ordinal')+'');
				books();
				$('label').css('color' , '#8f8f94');
				$(this).find('label').css('color' , 'green')
			}else{
				plus.nativeUI.confirm('温馨提示:购买后可以阅读全部书籍哦' , function(e){
					if(e.index == 0){
						mui.back();
					}
				} , '电子阅读' , ['确定' , '取消'])
			}
		});
	} 
	
	
	
	if(fontsize){ 
		$('#content').css('font-size' , fontsize);
		$('#content p').css('font-size' , fontsize); 
		$('.font_size button').css('border-color' , '#ffffff');
		if(fontsize == '16px'){
			$('.font_size button').eq(0).css('border-color' , '#139f13');
		}else if(fontsize == '14px'){
			$('.font_size button').eq(1).css('border-color' , '#139f13');
		}else{
			$('.font_size button').eq(2).css('border-color' , '#139f13');
		} 
	}
	
	if(fontfamily){ 
		$('#content').css('font-family' , fontfamily);
		$('#content p').css('font-family' , fontfamily); 
		$('.font_face button').css('border-color' , '#ffffff');
		if(fontfamily == 'timesNewRoman'){
			$('.font_face button').eq(0).css('border-color' , '#139f13');
		}else if(fontfamily == 'Arial'){
			$('.font_face button').eq(1).css('border-color' , '#139f13');
		}else{
			$('.font_face button').eq(2).css('border-color' , '#139f13');
		} 
	}
	
}

/**
 * 长按
 */
window.addEventListener('longtap' , function(){
	if($('.footer-alert1').is(':hidden')){
		$('.header').slideToggle() 
		$('.footer-alert').slideToggle()
	}
})

/**
 * 翻页
 * @param {Int} id [书籍id]
 * @param {Int} ordinal [当前章节id]  
 * @param {String} next/previous [执行操作] 
 */
function books(type){
	var ordinal = plus.storage.getItem('chapter'+id);
	$.ajax({  
		type : 'post',
		url : apiRoot + '/Home/Chapter/chapterDetails/',
		data : { 
			id : id,
			ordinal : ordinal,
			type : type 
		},
		beforeSend : function(){ 
			plus.nativeUI.showWaiting();
		},
		complete: function(){
			plus.nativeUI.closeWaiting();
		},
		dataType:'json', 
		success : function(data){  
			if(data.single){
				plus.storage.setItem('chapter'+id , data.single.ordinal+'');
				$('.name').text(data.single.chapter_name)
				$('#content').html(data.single.content);
				window.scrollTo( 0, 0 ); 
				$('label').css('color' , '#8f8f94');
				$('.chapter'+data.single.ordinal+' label').css('color' , 'green')
			}else{
				if(ordinal == 1 && type == 'previous'){
					toast('这是第一章')
				}else if(type == 'next'){
					toast('这是最后一章')
				}
			}
		},
		error : function(e){ 
			errorPrompt(e); 
		}
	})
}